<?php
$event_log = array();

function dump(){
	echo "<pre style='white-space:pre-line'>";
	$arg_list = func_get_args();
	foreach($arg_list as $key=>$var){
		var_dump($var);
	}
	echo "</pre>";
}

function block($value){
	return mysql_real_escape_string($value);
}

function one_file($files,$index=0){
	$file = array();

	$file['name'] = $files['name'][$index];
	$file['tmp_name'] = $files['tmp_name'][$index];
	$file['size'] = $files['size'][$index];
	$file['error'] = $files['error'][$index];

	return $file;
}

function files_var($files){
	$return = array();
	foreach ($files['name'] as $key => $value) {
		$return[] = array(
			'name'		=> $files['name'][$key],
			'type'		=> $files['type'][$key],
			'tmp_name'	=> $files['tmp_name'][$key],
			'error'		=> $files['error'][$key],
			'size'		=> $files['size'][$key]
		);
	}
	return $return;
}

function array_set_key($array,$key) {
	$return = array();
	foreach($array as $i=>$item){
		if (array_key_exists($key,$item)){
			$return[$item[$key]] = $item;
		}else{
			$return[$i] = $item;
		}
	}
	return $return;
}

function file_extension($file){
	$arr = explode('.',$file);
	return array_pop($arr);
}

function get_slug($slug){
	$slug = ereg_replace("(À|Á|Â|Ã|Ä|Å|à|á|â|ã|ä|å)","a",$slug);
	$slug = ereg_replace("(È|É|Ê|Ë|è|é|ê|ë)","e",$slug);
	$slug = ereg_replace("(Ì|Í|Î|Ï|ì|í|î|ï)","i",$slug);
	$slug = ereg_replace("(Ò|Ó|Ô|Õ|Ö|Ø|ò|ó|ô|õ|ö|ø)","o",$slug);
	$slug = ereg_replace("(Ù|Ú|Û|Ü|ù|ú|û|ü)","u",$slug);
	$slug = ereg_replace("(Ñ|ñ)","n",$slug);
	$slug = strtolower(trim($slug));
	$slug = preg_replace('/[^a-z0-9-]/', '-', $slug);
	$slug = preg_replace('/-+/', "-", $slug);
	$slug = trim($slug,'-');
	return $slug;
}

function check_email($email){
    if (preg_match("%^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$%", $email)) {
        return true;
    }
    return false;
}

function add_event($event_name,$description='',$class_name=''){
	global $event_log;
	$event_log[] = array(
		'event_name' => $event_name,
		'description'=> $description,
		'class_name' => $class_name
	);
}


function normalize_image($filename){
	$ext = explode(".",$filename);
	$ext = strtolower($ext[count($ext)-1]);

	if (in_array($ext,array('jpeg','jpg','gif','png'))){
		$conversion="convert ".$filename." +profile \"*\"  -type TrueColor -quality 85 ".$filename;
	}else{
		$conversion="convert ".$filename." +profile \"*\"  -type TrueColor -quality 85 ".basename($filename,".".$ext).".png";
	}
	//echo $conversion;
	exec($conversion);
}

function image_resize($file,$dest,$size){
	$return = false;
	mkdir(dirname($dest),0777,$recursive = true);
	if (empty($size['width'])){
		$size['width'] = 0;
	}
	if (empty($size['height'])){
		$size['height'] = 0;
	}

	$quality		= !$size['quality']	? 85 : (int)$size['qiality'];

	if (empty($size['crop']) || !$size['crop']){
		$newwidth	= !$size['width']		? '' : (int)$size['width'];
		$newheight	= !$size['height']	? '' : (int)$size['height'];
		$crop		= '';
	}else{
		list($width,$height)=getimagesize($filename);

		$newheight = $size['height'];
		$newwidth = (int)($newheight*$width/$height);
		if (($crop && $newwidth<$size['width']) || (!$crop && $newwidth>$maxwidth)){
			$newwidth = $size['width'];
			$newheight = (int)($newwidth*$height/$width);
		}
		$dx = abs((int)(($newwidth -$size['width'])/2));
		$dy = abs((int)(($newheight-$size['height'])/2));

		$crop = "-crop ".$size['width']."x".$size['height']."+$dx+$dy!";
	}

	$conversion="convert $filename -resize '".$newwidth."x".$newheight."' +profile \"*\" $crop -type TrueColor -quality $quality $dest";

	$r1 = "";
	$r2 = "";

	exec($conversion);
	if (file_exists($dest)){
		$return = true;
	}else{
		$return = false;
	}
	chmod($dest,0777);
	return $return;
}

function thickbox($url,$callback,$innerHTML,$title='',$get='',$classes=array(),$outterHTML=''){
	if (is_array($get)){
		$get['width']	= isset($get[0]) ? (int)$get[0] : (int)$get['width'];
		$get['height']	= isset($get[1]) ? (int)$get[1] : (int)$get['height'];
		unset($get[0]);
		unset($get[1]);
		$get_string = array();
		foreach($get as $key=>$value){
			$get_string []= "$key=".urlencode($value)."";
		}
		$get_string = implode('&',$get_string);
	}else{
		$get_string = empty($get) ? "width=300&height=200" : $get;
	}
	if (!in_array('thickbox',$classes)){
		$classes[] = 'thickbox';
	}
	$class = implode(' ',$classes);
	$url = url."ajax/".$url."?$get_string";

	$return = "<a class=\"$class\" href=\"$url\" title=\"".htmlspecialchars($title)."\" onclick=\"var sender = this;callback_function = ".block($callback).";\" ".block($outterHTML).">$innerHTML</a>";
	return $return;
}

function the_thickbox($url,$callback,$innerHTML,$title='',$get='',$classes=array(),$outterHTML=''){
	echo thickbox($url,$callback,$innerHTML,$none,$get,$classes,$outterHTML);
}

function the_link_url($page) {
	echo url . $page;
}

function percent_block($percent=0, $classes=""){
$percent = (int)$percent;
?>
	<div class="percents <?echo $classes;?>">
		<div style="width:<?echo $percent;?>%;" class="fill"></div>
		<div class="text"><?echo $percent?>%</div>
	</div>
<?php
}

function sort_rand($a,$b){
	return rand(0,2)-1;
}
/**/
?>
